package com.example.springboot.controller.user;

import com.example.springboot.bean.HttpResult;
import com.example.springboot.bean.UserInfo;
import com.example.springboot.oauth2.entity.User;
import com.example.springboot.oauth2.jpa.UserJPA;
import com.example.springboot.utils.Constant;
import com.example.springboot.utils.ResultUtil;
import com.example.springboot.utils.ValidateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
public class LoginController {

	@Autowired
	private UserJPA userRepository;

	@Autowired
	private PasswordEncoder passwordEncoder;

	@RequestMapping(value="/login/name",method=RequestMethod.POST)
    public HttpResult loginByName(@RequestParam String username, @RequestParam String password, HttpServletRequest req) {

        if(!ValidateUtil.get().matchUsername(username)){
            return ResultUtil.get().error(Constant.CODE_USERNAME_INVALIDATED, "UserName Invalidated");
        }

		User userFromDatabase = userRepository.findByUsernameCaseInsensitive(username);
		//用户不存在
		if (userFromDatabase == null){
			return ResultUtil.get().error(Constant.CODE_USER_EXIST_NO, "User Not Exist");
		}

		//clear session
		req.getSession().removeAttribute("User");

		//密码不匹配
		if (!passwordEncoder.matches(password, userFromDatabase.getPassword())){
			return ResultUtil.get().error(Constant.CODE_PASSWORD_NOT_MATCH, "Password Not Match");
		}

		//密码匹配
		//save session
		req.getSession().setAttribute("User", username);


		UserInfo userInfo = new UserInfo();
		userInfo.setUsername(userFromDatabase.getUsername());
		userInfo.setEmail(userFromDatabase.getEmail());
		userInfo.setPhone(userFromDatabase.getPhone());
        return ResultUtil.get().success(userInfo);
    }

	@RequestMapping(value="/login/email",method=RequestMethod.POST)
	public HttpResult loginByEmail(@RequestParam String email, @RequestParam String password, HttpServletRequest req) {

		if(!ValidateUtil.get().matchEmail(email)){
			return ResultUtil.get().error(Constant.CODE_EMAIL_INVALIDATED, "Email Invalidated");
		}


		User user = userRepository.findByEmailCaseInsensitive(email);
		//用户未注册
		if (user == null){
			return ResultUtil.get().error(Constant.CODE_USER_EXIST_NO, "User Not Exist");
		}

		//clear session
		req.getSession().removeAttribute("User");

		//密码不匹配
		if (!passwordEncoder.matches(password, user.getPassword())){
			return ResultUtil.get().error(Constant.CODE_PASSWORD_NOT_MATCH, "Password Not Match");
		}

		//密码匹配
		//save session
		req.getSession().setAttribute("User", user.getUsername());


		UserInfo userInfo = new UserInfo();
		userInfo.setUsername(user.getUsername());
		userInfo.setEmail(user.getEmail());
		userInfo.setPhone(user.getPhone());
		return ResultUtil.get().success(userInfo);
	}


	@RequestMapping(value="/login/phone",method=RequestMethod.POST)
	public HttpResult loginByPhone(@RequestParam String phone, @RequestParam String password, HttpServletRequest req) {

		if(!ValidateUtil.get().matchPhone(phone)){
			return ResultUtil.get().error(Constant.CODE_PHONE_INVALIDATED, "Phone Invalidated");
		}

		User user = userRepository.findByPhone(phone);
		//用户未注册
		if (user == null){
			return ResultUtil.get().error(Constant.CODE_USER_EXIST_NO, "User Not Exist");
		}

		//clear session
		req.getSession().removeAttribute("User");

		//密码不匹配
		if (!passwordEncoder.matches(password, user.getPassword())){
			return ResultUtil.get().error(Constant.CODE_PASSWORD_NOT_MATCH, "Password Not Match");
		}

		//密码匹配
		//save session
		req.getSession().setAttribute("User", user.getUsername());


		UserInfo userInfo = new UserInfo();
		userInfo.setUsername(user.getUsername());
		userInfo.setEmail(user.getEmail());
		userInfo.setPhone(user.getPhone());
		return ResultUtil.get().success(userInfo);
	}

	
	@RequestMapping(value="/logout",method=RequestMethod.GET)
    public HttpResult logout(@RequestParam String username ,HttpServletRequest req) {

		//clear session
		req.getSession().removeAttribute("User");
        return ResultUtil.get().success();
    }


}
